package asapIs134CK.womi.client.profile.admintools;



import java.util.Iterator;
import java.util.List;

import asapIs134CK.womi.client.profile.RPCUserService;
import asapIs134CK.womi.client.profile.RPCUserServiceAsync;
import asapIs134CK.womi.client.profile.UserClient;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class UserManagmentToolWidget extends Composite {

	private Panel _container;
	private AdminTable _table;

	private final RPCUserServiceAsync userService = GWT
	.create(RPCUserService.class);

	private class AdminTable extends Composite{
		private FlexTable _innerTable;
		private int _rowCount;
		private boolean _enabled;
		private List<UserClient> _userList;

		public AdminTable(){
			_enabled = false;
			_innerTable = new FlexTable();
			_innerTable.setText(0, 0, "nickname");
			_innerTable.setText(0, 1, "email");
			_innerTable.setText(0, 2, "Total Score");
			_innerTable.setText(0, 3, "Froggit Score");
			_innerTable.setText(0, 4, "Sudoku Score");
			_innerTable.setText(0, 5, "Arithmetic Score");
			_innerTable.setText(0, 6, "Admin");
			_innerTable.setText(0, 7, "Test Creator");
			_rowCount=0;
			//TODO add other field is needed
			initWidget(_innerTable);
			userService.getAll(new AsyncCallback<List<UserClient>>() {

				@Override
				public void onSuccess(List<UserClient> result) {
					fillWithUser(result);
					_userList = result;

				}

				@Override
				public void onFailure(Throwable caught) {
					Window.alert("The server could not be reached, please check your network connection.");
				}
			});
		}

		private void fillWithUser(List<UserClient> userList){
			Iterator<UserClient> ite = userList.iterator();
			while(ite.hasNext()){
				final UserClient user = ite.next();
				_innerTable.insertRow(++_rowCount);
				final TextBox nicknameField = new TextBox();
				nicknameField.setText(user.getNickname());
				nicknameField.setEnabled(_enabled);
				_innerTable.setWidget(_rowCount, 0, nicknameField);
				final TextBox emailField = new TextBox();
				emailField.setText(user.getEmail());
				emailField.setEnabled(_enabled);
				_innerTable.setWidget(_rowCount, 1, emailField);
				//TODO add text for the other field to create


				final Label totalScoreField = new Label();
				totalScoreField.setText(user.getScore()+"");
				_innerTable.setWidget(_rowCount, 2, totalScoreField);

				final TextBox FTFScoreField = new TextBox();
				FTFScoreField.setText(user.getScoreFroggit()+"");
				FTFScoreField.setEnabled(_enabled);
				ChangeHandler frogChangeHandler = new ChangeHandler(){

					@Override
					public void onChange(ChangeEvent event) {
						try {
							Integer.parseInt(FTFScoreField.getText());
						}
						catch(Exception e) {
							FTFScoreField.setText(user.getScoreFroggit()+"");
						}
					}
					
				};
				FTFScoreField.addChangeHandler(frogChangeHandler );
				_innerTable.setWidget(_rowCount, 3, FTFScoreField);

				final TextBox SudokuScoreField = new TextBox();
				SudokuScoreField.setText(user.getScoreSudoku()+"");
				SudokuScoreField.setEnabled(_enabled);
				ChangeHandler sudokuChangeHandler = new ChangeHandler(){

					@Override
					public void onChange(ChangeEvent event) {
						try {
							Integer.parseInt(SudokuScoreField.getText());
						}
						catch(Exception e) {
							SudokuScoreField.setText(user.getScoreSudoku()+"");
						}
					}
					
				};
				SudokuScoreField.addChangeHandler(sudokuChangeHandler );
				_innerTable.setWidget(_rowCount, 4, SudokuScoreField);
				
				final TextBox ArithField = new TextBox();
				ArithField.setText(user.getScoreArithmeticQuizz()+"");
				ArithField.setEnabled(_enabled);
				ChangeHandler arithChangeHandler = new ChangeHandler(){

					@Override
					public void onChange(ChangeEvent event) {
						try {
							Integer.parseInt(ArithField.getText());
						}
						catch(Exception e) {
							ArithField.setText(user.getScoreArithmeticQuizz()+"");
						}
					}
					
				};
				ArithField.addChangeHandler(arithChangeHandler );
				_innerTable.setWidget(_rowCount, 5, ArithField);
				
				final CheckBox isAdmin = new CheckBox();
				isAdmin.setChecked(user.isAdmin());
				isAdmin.setEnabled(_enabled);
				_innerTable.setWidget(_rowCount, 6, isAdmin);
				
				final CheckBox isTest = new CheckBox();
				isTest.setChecked(user.isTestCreator());
				isTest.setEnabled(_enabled);
				_innerTable.setWidget(_rowCount, 7, isTest);

				_innerTable.setWidget(_rowCount, 8, new Button("Edit",new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						nicknameField.setEnabled(true);
						FTFScoreField.setEnabled(true);
						SudokuScoreField.setEnabled(true);
						ArithField.setEnabled(true);
						isAdmin.setEnabled(true);
						isTest.setEnabled(true);
					}
				}));
				
				_innerTable.setWidget(_rowCount, 9, new Button("valide", new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						nicknameField.setEnabled(false);
						emailField.setEnabled(false);
						FTFScoreField.setEnabled(false);
						SudokuScoreField.setEnabled(false);
						ArithField.setEnabled(false);
						isAdmin.setEnabled(false);
						isTest.setEnabled(false);
						
						UserClient currUser = _userList.get(_rowCount-1);
						currUser.setNickname(nicknameField.getText());
						currUser.setScoreFroggit(Integer.parseInt(FTFScoreField.getText()));
						currUser.setScoreArithmeticQuizz(Integer.parseInt(ArithField.getText()));
						currUser.setScoreSudoku(Integer.parseInt(SudokuScoreField.getText()));
						currUser.setAdmin(isAdmin.isChecked());
						currUser.setTestCreator(isTest.isChecked());
						
						totalScoreField.setText(currUser.getScore()+"");
						//then send the information to the server
						userService.modifyUser(currUser, new AsyncCallback<Boolean>() {

							@Override
							public void onFailure(Throwable caught) {
								Window.alert("The server could not be reached, please check your network connection.");
							}

							@Override
							public void onSuccess(Boolean result) {
								System.out.println("modif apply successfully");
							}
						});
					}
				}));
			}
		}

		public void setEnabled(boolean enable){
			if(enable == _enabled){
				//do nothing :D
			}else{
				for(int i=1; i<=_rowCount; i++){
					for(int j=0; j<8 ; j++){
						Widget cellWidget = _innerTable.getWidget(i, j);
						if (cellWidget instanceof TextBox) {
							TextBox textbox = (TextBox) cellWidget;
							textbox.setEnabled(enable);
						}else if (cellWidget instanceof CheckBox) {
							CheckBox checkBox = (CheckBox) cellWidget;
							checkBox.setEnabled(enable);
						}
					}
				}
			}
		}

	}

	public UserManagmentToolWidget(){
		_container = new VerticalPanel();
		_table = new AdminTable();
		_table.setStyleName("AdminTable");
		_container.add(_table);
		initWidget(_container);
	}

}
